package com.start.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.reggie.entity.Orders;
import com.start.reggie.entity.ShoppingCart;
import com.start.reggie.service.OrderService;
import com.start.reggie.utils.BaseContext;
import com.start.reggie.utils.R;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);

        orderService.submit(orders);

        return R.success("下单成功");

    }

    /**
     * 订单明细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String number,String beginTime,String endTime){ //名字要和前端一样
        log.info("page={},pageSize={}", page, pageSize);

        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.like(number != null,Orders::getNumber,number)
                .ge(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .le(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);

        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> orderStatus(@RequestBody Orders orders){
        orderService.updateById(orders);

        return R.success("订单状态修改成功");
    }

    /**
     * 用户订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        log.info("page={},pageSize={}", page, pageSize);

        Page pageInfo = orderService.userPage(page,pageSize);

        return R.success(pageInfo);
    }

    /**
     * 用户再来一单
     * @param shoppingCart
     * @return
     */
    @PostMapping("/again")
    public R<String> againSubmit(@RequestBody ShoppingCart shoppingCart){
        Long id = shoppingCart.getId();

        orderService.againSubmit(id);

        return R.success("操作成功");
    }

}
